// JavaScript Document
$(function(){
    var id;
    var order;//标记用户点击的信息索引值，为信息的修改删除做定位标记
    //1，点击删除按钮删除相应信息词条
    $('#f-table').on('click','.bg-color2',function(){
        // id = $(this)('#f-table').val();
        id =$(this).parents('tr').attr('class');
    })

    $('#delete').click(function () {
        $.ajax({
            type: "POST",
            url: "../station/deleteStation",
            dataType:'json',
            data:{order:id},
            success: function(data)
            {
                if (data.status==200){
                    // alert(data.msg)
                }else {
                    alert(data.msg)
                }
                window.location.href = "../station/getStation";
            }
        });
    });

    //添加
    $('#addStation').click(function () {
        var station  = {};
        var stationCode = $('.num-input').val();             //场站编号
        var name = $('.name-input').val();	 		//场站名称
        var clentName = $('.owner-input').val();//所属公司
        var area = "";
        $('.span8 input').each(function(){
            var a = $(this).val();
            area += a + ",";
        });

        var strs= new Array();

        var strs2= new Array();

        strs = area.split(",")

        for (i=0;i<strs.length ;i++ )
        {
            if (strs[i]!==""){
                strs2.push(strs[i]);
            }
        }
        if (strs2.length<8){
            alert("最少填入四个标点！！")
            return;
        }
        station["stationCode"] = stationCode;
        station["name"] = name;
        station["clentName"] = clentName;
        station["area"] = area;
        $.ajax({
            type: "POST",
            url: "../station/addStation",
            dataType:'json',
            data:station,
            success:function(data){
                if (data==0){
                    // alert("添加成功")
                }else if (data==1){
                    alert("添加失败")
                }else {
                    alert("系统异常")
                }
                window.location.href = "../station/getStation";
            }
        });
    });
    //6，确定修改时定位被修改项：
    $('#f-table').on('click','.rewrite',function(){
        id =$(this).parents('tr').attr('class');
    });

    //修改信息后保存操作：
    $('#updateStation').click(function () {

        var name = $('.name-input').val();	 		//场站名称
        var clentName = $('.owner-input').val();		 //所属公司
        var stationCode = $('.num-input').val();	 		//场站编号
        var area = "";
        $('.span8 input').each(function(){
            var a = $(this).val();
            area += a + ",";
        });

        var strs= new Array();

        var strs2= new Array();

        strs = area.split(",")

        for (i=0;i<strs.length ;i++ )
        {
            if (strs[i]!==""){
                strs2.push(strs[i]);
            }
        }
        if (strs2.length<8){
            alert("最少填入四个标点！！")
            return;
        }
        var station  = {};
        station["id"] = id;
        station["name"] = name;
        station["clentName"] = clentName;
        station["area"] = area;
        station["stationCode"] = stationCode;
        $.ajax({
            type: "POST",
            url: "../station/updateStation",
            dataType:'json',
            data:station,
            success:function(data){
                window.location.href = "../station/getStation";
            }
        });
    });
    //
    // $('#souSuoStation').click(function () {
    //     var search = $('.search-query').val();
    //     location.href="../station/souSuoStation?word="+search;
    // });

})

function doUploadFile() {
    var formData = new FormData($( "#uploadFormFile" )[0]);
    $.ajax({
        type: 'POST',
        url: '../file/upload' ,
        data: formData,
        async: false,
        cache: false,
        contentType: false,
        processData: false,
        success: function (returndata) {
            var josn = JSON.stringify(returndata);
            var filePath = returndata.data;
            alert("上传成功");
            $.ajax({
                type: "POST",
                url: "../station/excelSaveCar",
                dataType:formData,
                data:{path:filePath},
                success: function(returndata){
                    var success  = JSON.stringify(returndata);
                    alert(JSON.stringify(returndata));
                },
                error: function (returndata) {
                    alert("文件导入失败");
                }
            });//ajax套用
        },
        error: function (returndata) {
            alert("上传失败");
        }
    });
}

$("input[type='file']").change(function(){
    var path = $('#filePath').val();
    $("#fileid").val(path);


    function getObjectURL(file) {
        var url = null;
        if (window.createObjcectURL != undefined) {
            url = window.createOjcectURL(file);
        } else if (window.URL != undefined) {
            url = window.URL.createObjectURL(file);
        } else if (window.webkitURL != undefined) {
            url = window.webkitURL.createObjectURL(file);
        }
        return url;
    }
    filePathTrue = getObjectURL(this.files[0]);//这里的objURL就是input file的真实路径
    //$("#fileid").val(objURL);
});


// $('#souSuoStation2').blur(function () {
//     var search = $('.search-query').val();
//     location.href="../station/souSuoStation?word="+search;
// });

function uploadStation(){
    var formData = new FormData($( "#uploadFormFile" )[0]);
    $.ajax({
        type: 'POST',
        url: '../station/exoutIn' ,
        data: formData,
        async: false,
        cache: false,
        contentType: false,
        processData: false,
        success:function(data){
            alert(data.msg)
            window.location.href = "../station/getStation";
        }
    });
}
